

package org.example.flinksql.source;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;

/**
 * Skeleton for a Flink Streaming Job.
 *
 * <p>For a tutorial how to write a Flink streaming application, check the
 * tutorials and examples on the <a href="https://flink.apache.org/docs/stable/">Flink Website</a>.
 *
 * <p>To package your application into a JAR file for execution, run
 * 'mvn clean package' on the command line.
 *
 * <p>If you change the name of the main class (with the public static void main(String[] args))
 * method, change the respective entry in the POM.xml file (simply search for 'mainClass').
 */
public class Collection {

	public static void main(String[] args) throws Exception {
		// set up the streaming execution environment
		final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

		/*
		 * Here, you can start creating your execution plan for Flink.
		 *
		 * Start with getting some data from the environment, like
		 * 	env.readTextFile(textPath);
		 *
		 * then, transform the resulting DataStream<String> using operations
		 * like
		 * 	.filter()
		 * 	.flatMap()
		 * 	.join()
		 * 	.coGroup()
		 *
		 * and many more.
		 * Have a look at the programming guide for the Java API:
		 *
		 * https://flink.apache.org/docs/latest/apis/streaming/index.html
		 *
		 */


		DataStreamSource<String> source = env.fromElements("hello", "world", "hello", "flink");
		source.print();

		DataStreamSource<String> source2 = env.fromElements(String.class, "hello", "world", "hello", "flink");
		source2.print();

		Book java = new Book("java", "java book", 1);
		Book go = new Book("go", "go book", 2);
		Book flink = new Book("flink", "flink book", 3);

		DataStreamSource<Book> source3 = env.fromElements(Book.class, java, go, flink);
		source3.print();

		ArrayList<Book> books  = new ArrayList<>();
		books.add(java);
		books.add(go);
		books.add(flink);
		DataStreamSource<Book> bookSource = env.fromCollection(books);
		bookSource.print();

		SingleOutputStreamOperator<Book> operator = bookSource.filter((FilterFunction<Book>) book -> "go".equals(book.name));
		DataStreamSource<Book> goSource = new DataStreamSource<>(operator);
		goSource.print();

		// execute program
		env.execute("Flink Streaming Java API Skeleton");
	}

	private static class Book {
		public String name;
		public String desc;
		public int price;

		public Book() {}
		public Book(String name, String desc, int price) {
			this.name = name;
			this.desc = desc;
			this.price = price;
		}

		@Override
		public String toString() {
			return "Book{" +
					"name='" + name + '\'' +
					", desc='" + desc + '\'' +
					'}';
		}
	}
}
